package com.ts.proj.json;

import com.ts.base.controller.BaseController;
import com.ts.proj.entity.MeasureEntity;
import com.ts.proj.service.MeasureService;
import com.ts.util.DateUtil;
import com.ts.util.PageData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class StatisticsJson extends BaseController {

    @Autowired
    private MeasureService measureService;

    @RequestMapping(value = "/statics-list")
    public Map<String,Object> measureList(){

        PageData pageData = this.getPageData();
        String field = "";
        String groupBy = "";
        String param[] = new String[]{};
        StringBuilder sb1 = new StringBuilder();
        StringBuilder sb = new StringBuilder();
        if(null!=pageData.get("carId")&&!"".equals(pageData.getString("carId"))){
            groupBy += "  and t.car_id=  '" + pageData.getString("carId")+"'";
        }
        if(null!=pageData.get("from")&&!"".equals(pageData.getString("from"))){
            groupBy += " and left(t.create_time,8)>=  '" + pageData.getString("from").replaceAll("-","") + "'";
        }else if(null==pageData.get("from")||"".equals(pageData.getString("from"))){
            groupBy += " and left(t.create_time,8)>=  '" + DateUtil.getDays() + "'";
        }

        if(null!=pageData.get("to")&&!"".equals(pageData.getString("to"))){
            groupBy += " and left(t.create_time,8)<=  '" + pageData.getString("to").replaceAll("-","") + "'";
        }else if(null==pageData.get("to")||"".equals(pageData.getString("to"))){
            groupBy += " and left(t.create_time,8)<=  '" + DateUtil.getDays() + "'";
        }

        if(null!=pageData.get("param")&&!"".equals(pageData.getString("param"))){
            param = pageData.getString("param").split("@");
            sb1.append("  group by  ");
            for(int i=0;i<param.length;i++){
                String str = param[i];
                if(i==param.length-1){
                    if(str.contains("create_time")){
                        sb.append(" left(t.create_time,8) as createTime ");
                        sb1.append(" left(t.create_time,8) ");
                    }else{
                        sb.append("t." + param[i]);
                        sb1.append("t." + param[i]);
                    }

                }else{
                    if(str.contains("create_time")){
                        sb.append("  left(t.create_time,8) as createTime  , ");
                        sb1.append("  left(t.create_time,8)  , ");
                    }else{
                        sb.append(" t." +param[i]+" , ");
                        sb1.append(" t." +param[i]+" , ");
                    }
                }
            }
            field = ","+sb.toString();
            groupBy += sb1.toString();

        }
//        field = field.replaceAll("t.create_time", "left(t.create_time,8) as createTime");
//        groupBy = groupBy.replaceAll("t.create_time", "left(t.create_time,8)");
        List<PageData> measures = measureService.sumByGroup(param,field,groupBy);
        pageData.put("rows", measures);
        return pageData;
    }
}
